package com.dianping.cat.alarm.policy.transform;

import com.dianping.cat.alarm.policy.IEntity;
import com.dianping.cat.alarm.policy.IVisitor;
import com.dianping.cat.alarm.policy.entity.AlertPolicy;
import com.dianping.cat.alarm.policy.entity.Group;
import com.dianping.cat.alarm.policy.entity.Level;
import com.dianping.cat.alarm.policy.entity.Type;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;













public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

/*  33 */   public DefaultXmlBuilder() { this(false); }



/*  37 */   public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }

  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  41 */     this.m_compact = compact;
/*  42 */     this.m_sb = sb;
/*  43 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }

  public String buildXml(IEntity<?> entity) {
/*  47 */     entity.accept(this.m_visitor);
/*  48 */     return this.m_sb.toString();
  }

  protected void endTag(String name) {
/*  52 */     this.m_level--;

/*  54 */     indent();
/*  55 */     this.m_sb.append("</").append(name).append(">\r\n");
  }


/*  59 */   protected String escape(Object value) { return escape(value, false); }


  protected String escape(Object value, boolean text) {
/*  63 */     if (value == null) {
/*  64 */       return null;
    }

/*  67 */     String str = toString(value);
/*  68 */     int len = str.length();
/*  69 */     StringBuilder sb = new StringBuilder(len + 16);

/*  71 */     for (int i = 0; i < len; i++) {
/*  72 */       char ch = str.charAt(i);

/*  74 */       switch (ch) {
        case '<':
/*  76 */           sb.append("&lt;");
          break;
        case '>':
/*  79 */           sb.append("&gt;");
          break;
        case '&':
/*  82 */           sb.append("&amp;");
          break;
        case '"':
/*  85 */           if (!text) {
/*  86 */             sb.append("&quot;");
            break;
          }
        default:
/*  90 */           sb.append(ch);
          break;
      }

    }
/*  95 */     return sb.toString();
  }

  protected void indent() {
/*  99 */     if (!this.m_compact) {
/* 100 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 101 */         this.m_sb.append("   ");
      }
    }
  }


/* 107 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 111 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 115 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }


  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 119 */     indent();

/* 121 */     this.m_sb.append('<').append(name);

/* 123 */     int len = nameValues.length;

/* 125 */     for (int i = 0; i + 1 < len; i += 2) {
/* 126 */       Object attrName = nameValues[i];
/* 127 */       Object attrValue = nameValues[i + 1];

/* 129 */       if (attrValue != null) {
/* 130 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 134 */     if (dynamicAttributes != null) {
/* 135 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 136 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }

/* 140 */     if (text != null && closed) {
/* 141 */       this.m_sb.append('>');
/* 142 */       this.m_sb.append(escape(text, true));
/* 143 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 145 */       if (closed) {
/* 146 */         this.m_sb.append('/');
      } else {
/* 148 */         this.m_level++;
      }

/* 151 */       this.m_sb.append(">\r\n");
    }
  }


  protected String toString(Object value) {
/* 157 */     if (value instanceof String)
/* 158 */       return (String)value; 
/* 159 */     if (value instanceof Collection) {
/* 160 */       Collection<Object> list = (Collection)value;
/* 161 */       StringBuilder sb = new StringBuilder(32);
/* 162 */       boolean first = true;

/* 164 */       for (Object item : list) {
/* 165 */         if (first) {
/* 166 */           first = false;
        } else {
/* 168 */           sb.append(',');
        }

/* 171 */         if (item != null) {
/* 172 */           sb.append(item);
        }
      }

/* 176 */       return sb.toString();
/* 177 */     }  if (value.getClass().isArray()) {
/* 178 */       int len = Array.getLength(value);
/* 179 */       StringBuilder sb = new StringBuilder(32);
/* 180 */       boolean first = true;

/* 182 */       for (int i = 0; i < len; i++) {
/* 183 */         Object item = Array.get(value, i);

/* 185 */         if (first) {
/* 186 */           first = false;
        } else {
/* 188 */           sb.append(',');
        }

/* 191 */         if (item != null) {
/* 192 */           sb.append(item);
        }
      }

/* 196 */       return sb.toString();
    }

/* 199 */     return String.valueOf(value);
  }


  public void visitAlertPolicy(AlertPolicy alertPolicy) {
/* 204 */     startTag("alert-policy", null, new Object[0]);

/* 206 */     if (!alertPolicy.getTypes().isEmpty()) {
/* 207 */       for (Type type : alertPolicy.getTypes().values()) {
/* 208 */         type.accept(this.m_visitor);
      }
    }

/* 212 */     endTag("alert-policy");
  }


  public void visitGroup(Group group) {
/* 217 */     startTag("group", null, new Object[] { "id", group.getId() });

/* 219 */     if (!group.getLevels().isEmpty()) {
/* 220 */       for (Level level : group.getLevels().values()) {
/* 221 */         level.accept(this.m_visitor);
      }
    }

/* 225 */     endTag("group");
  }



/* 230 */   public void visitLevel(Level level) { startTag("level", true, null, new Object[] { "id", level.getId(), "send", level.getSend(), "suspendMinute", level.getSuspendMinute(), "recoverMinute", level.getRecoverMinute() }); }



  public void visitType(Type type) {
/* 235 */     startTag("type", null, new Object[] { "id", type.getId() });

/* 237 */     if (!type.getGroups().isEmpty()) {
/* 238 */       for (Group group : type.getGroups().values()) {
/* 239 */         group.accept(this.m_visitor);
      }
    }

/* 243 */     endTag("type");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-alarm-3.0.0.jar!/com/dianping/cat/alarm/policy/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */